package lumis.service.wsrp.common;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.sun.portal.wsrp.common.LeaseTime;
import com.sun.portal.wsrp.common.stubs.v2.Lifetime;

/**
 * WSRP utility class.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class WSRPUtils 
{
	/**
	 * Array of strings containing the default possible windowStates to be used with WSRP.
	 */
	public static final String[] WSRP_WINDOWS_STATES = new String[]{"wsrp:maximized", "wsrp:minimized", "wsrp:normal"};
	/**
	 * Array of strings containing the default possible portletModes to be used with WSRP.
	 */
	public static final String[] WSRP_PORTLET_MODES = new String[]{"wsrp:view", "wsrp:edit", "wsrp:help"};
	public static final long MILISSECONDS_IN_A_DAY = 1000 * 60 * 60 * 24;
	
	/**
	 * Converts a date, in milliseconds, into a XMLGregorianCalendar.
	 * @param long representing the desired date. 
	 * @return date the date in XMLGregorianCalendar format.
	 */
	public static XMLGregorianCalendar toXMLGregorianCalendar(long date) 
	{
		DatatypeFactory dataTypeFactory;
		try 
		{
			dataTypeFactory = DatatypeFactory.newInstance();
		} 
		catch (DatatypeConfigurationException e) 
		{
			throw new RuntimeException(e);
		}
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTimeInMillis(date);
		return dataTypeFactory.newXMLGregorianCalendar(gc);
	}
	/**
	 * Converts a date, into a XMLGregorianCalendar.
	 * @param date to be converted.
	 * @return date in the XMLGregorianCalendar format.
	 */
	public static XMLGregorianCalendar toXMLGregorianCalendar(Date date)
	{
		return toXMLGregorianCalendar(date.getTime());
	}
	
	/**
	 * Converts a XMLGregorianCalendar into a date, in milliseconds. 
	 * @param calendar XMLGregorianCalendar format of the date.
	 * @return date converted to milliseconds. 
	 */
	public static long fromXMLGregorianCalendar(XMLGregorianCalendar calendar)
	{
		return calendar.toGregorianCalendar().getTimeInMillis();
	}
	
	/**
	 * Converts a XMLGregorianCalendar into a Date object. 
	 * @param calendar XMLGregorianCalendar format of the date.
	 * @return date as java.lang.Date object.
	 */
	public static Date dateFromXMLGregorianCalendar(XMLGregorianCalendar calendar)
	{
		if(calendar != null)
		{
			return calendar.toGregorianCalendar().getTime();
		}
		return null;
	}
	
	/**
	 * Joins a list into a String with its values separated by the delimiter.
	 * @param list of CharSequence to join into a single string. 
	 * @param delimiter used to join the CharSequences.
	 * @return list in the form of a string.
	 */
    public static String join(List<? extends CharSequence> s, String delimiter) 
    {
    	StringBuilder buffer = new StringBuilder();
    	boolean first = true;
    	for (CharSequence charSequence : s) 
    	{
			if(!first)
			{
				buffer.append(delimiter);
			}
			else
			{
				first = false;
			}
			buffer.append(charSequence);
		}
    	return buffer.toString();
    }  
    
    /**
     * Creates a Lifetime object and sets its termination time converting the Date instance to a XMLGregorianCalendar.
     * @param date	an instance of Date.
     * @return	an instance of Lifetime.
     */
    public static Lifetime lifetimeTerminationTimeFromDate(Date date)
    {
    	Lifetime lifetime = null;
    	if (date != null)
    	{
        	XMLGregorianCalendar xgc = WSRPUtils.toXMLGregorianCalendar(date);
        	lifetime = new Lifetime();
        	lifetime.setTerminationTime(xgc);    		
    	}
    	return lifetime;
    }
    
    /**
     * Creates a Date object getting the terminationTime from a Lifetime object.
     * @param lifetime	an instance of Lifetime.
     * @return	an instance of Date.
     */
    public static Date lifetimeTerminationTimeToDate(Lifetime lifetime)
    {
    	if (lifetime != null)
		{
			return lifetime.getTerminationTime().toGregorianCalendar().getTime();
		}
		else
		{
			return null;
		}   	
    }
    
    /**
     * Converts a {@link LeaseTime} into a {@link Lifetime}.  The attribute refreshDuration will not be filled in the Lifetime. 
     * @param leasetime LeaseTime to be converted into a lifetime.
     * @return lifetime representation of the given LeaseTime. 
     */
    public static Lifetime convertLeasetimeToLifetime(LeaseTime leasetime)
    {
    	if(leasetime == null)
    		return null;
    	Lifetime lifetime = new Lifetime();
    	lifetime.setCurrentTime(leasetime.getCurrentTime());
    	lifetime.setTerminationTime(leasetime.getTerminationTime());
    	return lifetime;
    }
    
    /**
     * Converts a {@link Lifetime} into a {@link LeaseTime}.  
     * @param lifetime Lifetime to be converted.
     * @return resulting LeaseTime.
     */
    public static LeaseTime convertLifetimeToLeaseTime(Lifetime lifetime)
    {
    	if(lifetime == null)
    		return null;
    	LeaseTime lease = new LeaseTime();
    	lease.setCurrentTime(lifetime.getCurrentTime());
    	lease.setTerminationTime(lifetime.getTerminationTime());
    	return lease;
    }
}